package org.ki4.kipastelsys

import org.ki4.kipastelsys.auth.Pessoa
import grails.plugins.springsecurity.Secured;


class PedidoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    static int codigoDoPedido = 0
    def springSecurityService

    @Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def index = {
        redirect(action: "list", params: params)
    }

    @Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [pedidoInstanceList: Pedido.list(params), pedidoInstanceTotal: Pedido.count()]
    }

        def createScaffold = {
            def pedidoInstance = new Pedido()
            pedidoInstance.properties = params
            return [pedidoInstance: pedidoInstance]
        }

    @Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def save = {
        def pedidoInstance = new Pedido(params)
        if (pedidoInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'pedido.label', default: 'Pedido'), pedidoInstance.id])}"
            redirect(action: "show", id: pedidoInstance.id)
        }
        else {
            render(view: "create", model: [pedidoInstance: pedidoInstance])
        }
    }

    @Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def show = {
        def pedidoInstance = Pedido.get(params.id)
        if (!pedidoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
            redirect(action: "list")
        }
        else {
            [pedidoInstance: pedidoInstance]
        }
    }
    
    @Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def edit = {
        def pedidoInstance = Pedido.get(params.id)
        if (!pedidoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [pedidoInstance: pedidoInstance]
        }
    }
    
    @Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def update = {
        def pedidoInstance = Pedido.get(params.id)
        if (pedidoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (pedidoInstance.version > version) {

                    pedidoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
                        message(code: 'pedido.label', default: 'Pedido')]
                    as Object[], "Another user has updated this Pedido while you were editing")
                    render(view: "edit", model: [pedidoInstance: pedidoInstance])
                    return
                }
            }
            pedidoInstance.properties = params
            if (!pedidoInstance.hasErrors() && pedidoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'pedido.label', default: 'Pedido'), pedidoInstance.id])}"
                redirect(action: "show", id: pedidoInstance.id)
            }
            else {
                render(view: "edit", model: [pedidoInstance: pedidoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
            redirect(action: "list")
        }
    }
    
    @Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def delete = {
        def pedidoInstance = Pedido.get(params.id)
        if (pedidoInstance) {
            try {
                pedidoInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'pedido.label', default: 'Pedido'), params.id])}"
            redirect(action: "list")
        }
    }
    
    @Secured(['ROLE_FUNCIONARIO', 'ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def create = {
        def pedidoInstance = new Pedido()
        pedidoInstance.properties = params
        pedidoInstance.setAutor lookupPerson()
        if (pedidoInstance.save(flush: true)) {
            pedidoInstance.setCodigo geraCodigoDePedido()
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'pedido.label', default: 'Pedido'), pedidoInstance.id])}"
            redirect(action: "edit", id: pedidoInstance.id)
        }
        else {
            render(view: "create", model: [pedidoInstance: pedidoInstance])
        }
    }

    private int geraCodigoDePedido() {
        if (codigoDoPedido > 98)
            codigoDoPedido = 1
        else
            codigoDoPedido++
        return codigoDoPedido
    }
    
    private lookupPerson() {
        Pessoa.get(springSecurityService.principal.id)
    }
}
